import { getBaseUrl } from '@/services/urlconst';
import { requestFunComponFiex } from '@/utils';
import { useMemo } from 'react';
import { useRequest } from 'umi';
let baseUrl = getBaseUrl();
// let baseUrl = '/wenchuang'; // 本地环境

const getStoreTypeManage = (curRes: any) => {
  const requestTree = useRequest(
    (params: any) => {
      let url = `${baseUrl}/assetStore/tree`;
      return requestFunComponFiex('GET', url);
    },
    {
      manual: false,
      onSuccess: (res) => {},
    },
  );

  const reloadLeftTree = (params: any) => requestTree.run(params);

  const designTypeTree = useMemo(() => {
    if (requestTree.data?.msgFlag && requestTree.data?.rows?.length !== 0) {
      return requestTree.data?.rows;
    } else {
      return [];
    }
  }, [requestTree.data]);

  const designTypeLoading = requestTree.loading;

  return {
    reloadLeftTree,
    designTypeTree,
    designTypeLoading,
  };
};

/**
 * 获取异步数据类型
 */
const treeloadMdicMapServuces = (loadMdicMapCallback: (res: any) => void) => {
  const loadMdicMapService = useRequest(
    (dicTypeIDs: any) => {
      let url = `${baseUrl}/commonController/loadMdicMap`;
      let bodyParams = {
        dicTypeIDs: dicTypeIDs,
      };
      return requestFunComponFiex('POST', url, bodyParams);
    },
    {
      manual: true,
      onSuccess: loadMdicMapCallback,
    },
  );

  return {
    treeLoadMdicMapRun: loadMdicMapService?.run,
    treeLoadMdicMapLoading: loadMdicMapService?.loading,
  };
};

export {
  getStoreTypeManage, //左侧树获取仓库列表
  treeloadMdicMapServuces,
};
